$(function() {
  var lastNode = null;
  var createHchart = function() {
    var chart = echarts.init($("#totalHr").get(0));
    var option = {
      tooltip: {
        trigger: 'axis'
      },
      title: {
        text: '单位（MH）',
        left: 0,
        top: -5,
        textStyle: {
          fontWeight: 'normal',
          fontSize: 14
        }
      },
      grid: {
        left: '75',
        right: '0',
        top: '20',
        bottom: '40'
      },
      legend: {
        icon: 'rect',
        data: ['有效算力', '无效算力'],
        bottom: 'bottom',
        textStyle: {
          fontSize: 12
        }
      },
      xAxis: {
        type: 'category',
        boundaryGap: true,
        data: [],
        axisTick: {
          show: false
        },
        axisLabel: {
          show: false
        },
        axisLine: {
          show: false
        }
      },
      yAxis: {
        type: 'value',
        position: 'left',
        animation: true,
        scale: true
      },
      series: [{
        name: '总算力',
        type: 'line',
        barMaxWidth: '20',
        data: [],
        itemStyle: {
          normal: {
            color: '#1F77B4'
          }
        }
      }]
    };
    $.get(base_url + "/api/history-pool-hr", function(datas) {
      var ds = {
        hr: [],
        label: []
      };
      datas.forEach(function(item, idx) {
        if(coinType.toLowerCase() == "sc") {
            if(idx % 6 == 0) {
                ds.hr.push((item['pool_hashrate']/1000000).toFixed(2));
                ds.label.push(dateFormat(item.timestamp*1000, 'yyyy-MM-dd hh:mm:ss'));
            }
        } else {
            ds.hr.push((item['pool_hashrate']/1000000).toFixed(2));
            ds.label.push(dateFormat(item.timestamp*1000, 'yyyy-MM-dd'));
        }
      });
      if(ds.hr && ds.hr.length && (lastNode !== null)) {
          ds.hr.splice((ds.hr.length - 1), 1, ((lastNode/1000000).toFixed(2)));
      }
      chart.setOption(option);
      chart.setOption({
        xAxis: {
          data: ds.label
        },
        series: [
          { data: ds.hr }
        ]
      });
    });
  }
  $(".welcome-title").text(poolConfig[coinType].welcome);
  $(".nav.navbar-nav").children(":eq(0)").addClass("active").siblings(".active").removeClass("active");
  $.get(base_url + "/api/stats", function(data) {
    if(data.hashrate) lastNode = data.hashrate;
    $(".info-hashrate").text(unitCover(data.hashrate));
    $(".activeMiners").text(data.minersTotal)
    $(".block-total").text(data.maturedTotal)
    $.each(data.luck, function(key, item) {
      var $tr = $("<tr>");
      $("<td>").text(replaceHeaderInfo(key)).appendTo($tr);
      $("<td>").text((item.luck*100).toFixed(0) + "%").appendTo($tr)
      if(coinType!="SC") $("<td>").text((item.uncleRate*100).toFixed(0) + "%").appendTo($tr)
      $("<td>").text((item.orphanRate*100).toFixed(0) + "%").appendTo($tr)
      if(coinType!="SC") $("<td>").text(item.rewardPerMHSecond.toFixed(6) + " "+ poolConfig[coinType].coinUnit +" 每MH/s").appendTo($tr)
      $tr.appendTo($(".lucks"));
    });
    createHchart();
  })
  $.get(base_url + "/api/miners", function(data) {
    var count = 0;
    $(".hashrate-total").text(unitCover(data.hashrate))
    $(".miner-total").text(data.minersTotal)
    var miners = [];
    $.each(data.miners, function(key, item) {
      item.hash = key
      miners.push(item);
    })
    miners.sort(function(a, b) {
      return b.hr - a.hr;
    })
    miners = miners.splice(0, 20);
    $("#miner").tmpl(miners).appendTo(".miners");
    $('#minersTable').DataTable({
      "paging": false,
      "info": false,
      'bSort': false,
      "searching": false,
      "stateSave": true,
      'language': {
        "search": "搜索:"
      }
    });
  });
});

function replaceHeaderInfo(txt) {
  if (txt.indexOf("day") > 0) {
    txt = txt.replace("day", "天")
  } else if (txt.indexOf("week") > 0) {
    txt = txt.replace("week", "周")
  } else if (txt.indexOf("month") > 0) {
    txt = txt.replace("month", "月")
  }
  return txt
}
